<?php
define('CORE_PATH',str_replace('\\','/',dirname(__FILE__)));

//加载配置文件
require CORE_PATH.'/config.php';
ob_start();
//页面编码
header('Content-type:text/html;charset='.$sys_config['charset']);
//设置时区
date_default_timezone_set($sys_config['timezone']);

//自动加载类库文件
function autoload($path)
{
    if( is_dir( $path ) == true )
    {
        $files = scandir( $path );
        foreach( $files as $f_k => $f_v)
        {
            if( $f_v != '.' && $f_v != '..' && is_dir($path.$f_v) != true )
            {
                require $path.$f_v;
            }
        }
    }
    else
    {
        die('请检查config.php目录配置参数是否正确');
    }
}

autoload(CORE_PATH.'/../_plugins/');




if( ini_get('session.auto_start') != 1 && $sys_config['session'] == true )
{
    //开启SESSION
    Sessions::init();
}

/***
 * html转码特殊字符
 */
function h($string){ return htmlspecialchars($string, ENT_QUOTES); }

/***
 * html解码
 */
function h_d($string){ return htmlspecialchars_decode($string, ENT_QUOTES); }

/***
 * 截取字符串
 */
function cut_str($string,$length=10,$suffix='...')
{
    $array = array();

    $total_length = strlen($string);

    for($i=0;$i<$length;$i++)
    {
        if($i < $length)
        {
            if(ord($string[$i]) >= 128)
            {
                $array[] = $string[$i].$string[$i+1].$string[$i+2];
                $i += 2;
                $length += 1;
                $total_length -= 1;
            }
            else
            {
                $array[] = $string[$i];
            }
        }
    }

    if($total_length <= $length)
    {
        $suffix = '';
    }

    return implode('',$array).$suffix;
}


/***
 * 返回模板地址，默认_default
 */
function template($filename,$style_path='_default')
{
    global $root;
    $filename = $root.'_template/'.$style_path.'/'.$filename.'.php';
    if(!file_exists($filename))
    {
        echo '模板文件:[<span style="font-weight:bolder;color:#FF4500;">'.$filename.'</span>]不存在';
        exit;
    }
    return $filename;
}

/***
 * 页面跳转
 */
function redirect($url,$delay=0)
{
    $delay = intval($delay);
    if($delay>0)
    {
        echo <<<EOT
<html>
<head>
<meta http-equiv="refresh" content="{$delay};URL={$url}" />
</head>
</html>
EOT;
    }
    else
    {
        header("Location: {$url}");
    }
}

//判断UTF8编码
function is_utf8($data)
{
    if (preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$data) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$data) == true || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/",$data) == true)
    {
        return true;
    }
    else
    {
        return false;
    }
}


/*

//授权登陆验证
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="zgqw.com"');
header('HTTP/1.0 401 Unauthorized');
echo 'please login';
exit;
}

if( $_SERVER['PHP_AUTH_USER'] != 'admin' || $_SERVER['PHP_AUTH_PW'] != 'admin123' )
{
	header('WWW-Authenticate: Basic realm="zgqw.com"');
	header('HTTP/1.0 401 Unauthorized');
	echo 'please login';
	exit;
}
*/

